<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="static/h-ui/css/register.css">
    <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script> 
    
</head>
<body>
    <div class="top">
    <span class="all">*</span><label for="invoice">发票号</label>
    <input type="text" id="invoice" class="one">
    <input type="button" value="挂号" onclick="register()">
    <input type="button" value="清空" onclick="reset()">
</div><br>
     <div class="header">
         <p>挂号信息</p>
         <p>看诊日期&nbsp;&nbsp;&nbsp;<span id="nowDate"></span>&nbsp;&nbsp;<span id="noon"></span></p>
     </div><hr>
     <div>
         <span>病历号</span>
         <input type="text" class="one" id="caseNumber">
         <input type="button" value="病例查询" id="searchCase" onclick="searchCase()">
     </div><br>
     <div>
         <span>姓名&nbsp;&nbsp;&nbsp;</span>
         <input type="text" class="one" id="realName">
         <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;性别</span>
         <input type="radio" name="sex" id="male" value="男">男
         <input type="radio" name="sex" id="famale" value="女">女
         <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;出生日期</span>
         <input type="date" id="birthDate" onchange="getAge()">
         <span>&nbsp;&nbsp;&nbsp;年龄</span>
         <input type="text" class="two" id="age">&nbsp;&nbsp;&nbsp;
         <select name="" id="ageType">
             <option value="岁">岁</option>
             <option value="月">月</option>
             <option value="天">天</option>
         </select>
     </div><br>
     <div>
         <span>挂号级别</span>
         <select name="" id="registLevel">
             <option>请选择挂号级别</option>
             <option>专家号</option>
             <option>普通号</option>
         </select>
         <span>&nbsp;&nbsp;&nbsp;&nbsp;挂号科室</span>
         <select name="" id="dept">
                    <option value="">请选择挂号科室</option>
                    <option>心血管内科</option>
<option>神经内科</option>
<option>普通内科</option>
<option>消化内科</option>
<option>呼吸内科</option>
<option>内分泌科</option>
<option>肾病内科</option>
<option>血液内科</option>
<option>感染内科</option>
<option>老年病内></option>
<option>风湿免疫></option>
<option>透析科</option>
<option>变态反应科</option>
<option>普通外科</option>
<option>泌尿外科</option>
<option>神经外科</option>
<option>胸外科</option>
<option>整形外科</option>
<option>肛肠外科</option>
<option>肝胆外科</option>
<option>乳腺外科</option>
<option>心血管外</option>
<option>心脏外科</option>
<option>器官移植</option>
<option>微创外科></option>
<option>功能神经外科</option>
<option>腺体外科</option>
<option>儿科综合</option>
<option>小儿外科</option>
<option>儿童保健科</option>
<option>新生儿科</option>
<option>小儿骨科</option>
<option>小儿神经内科</option>
<option>小儿呼吸科</option>
<option>小儿血液科</option>
<option>小儿耳鼻喉科</option>
<option>小儿心内科</option>
<option>小儿康复科</option>
<option>小儿精神科</option>
<option>小儿肾内科</option>
<option>小儿消化科</option>
<option>小儿皮肤科</option>
<option>小儿急诊科</option>
<option>小儿内分泌科</option>
<option>小儿泌尿外科</option>
<option>小儿感染科</option>
<option>小儿心外科</option>
<option>小儿胸外科</option>
<option>小儿神经外科</option>
<option>小儿整形科</option>
<option>小儿风湿免疫科</option>
<option>小儿妇科</option>
<option>传染科</option>
<option>肝病科</option>
<option>艾滋病科</option>
<option>传染危重室</option>
<option>妇产科综合</option>
<option>妇科</option>
<option>产科</option>
<option>计划生育科</option>
<option>妇科内分泌</option>
<option>遗传咨询科</option>
<option>产前检查科</option>
<option>妇泌尿科</option>
<option>前列腺</option>
<option>性功能障碍</option>
<option>生殖器感染</option>
<option>男性不育</option>
<option>生殖整形</option>
<option>精神科</option>
<option>司法鉴定科</option>
<option>药物依赖科</option>
<option>中医精神科</option>
<option>双相障碍科</option>
<option>皮肤科</option>
<option>性病科</option>
<option>中医综合科</option>
<option>针灸科</option>
<option>中医骨科</option>
<option>中医妇产科</option>
<option>中医外科</option>
<option>中医儿科</option>
<option>中医肛肠科</option>
<option>中医皮肤科</option>
<option>中医五官科</option>
<option>中医按摩科</option>
<option>中医消化科</option>
<option>中医肿瘤科</option>
<option>中医心内科</option>
<option>中医神经内科</option>
<option>中医肾病内科</option>
<option>中医内分泌</option>
<option>中医呼吸科</option>
<option>中医肝病科</option>
<option>中医男科</option>
<option>中医风湿免疫内科</option>
<option>中医血液科</option>
<option>中医乳腺外科</option>
<option>中医老年病科</option>
<option>肿瘤综合科</option>
<option>肿瘤内科</option>
<option>放疗科</option>
<option>肿瘤外科</option>
<option>肿瘤妇科</option>
<option>骨肿瘤科</option>
<option>肿瘤康复科</option>
<option>骨外科</option>
<option>手外科</option>
<option>创伤骨科</option>
<option>脊柱外科</option>
<option>骨关节科</option>
<option>骨质疏松科</option>
<option>矫形骨科</option>
<option>耳鼻咽喉头颈科</option>
<option>口腔科</option>
<option>眼科</option>
<option>康复科</option>
<option>理疗科</option>
<option>麻醉科</option>
<option>疼痛科</option>
<option>营养科</option>
<option>高压氧科</option>
<option>功能检查科</option>
<option>病理科</option>
<option>检验科</option>
<option>实验中心</option>
<option>心电图科</option>
<option>放射科</option>
<option>超声诊断科</option>
<option>医学影像科</option>
<option>核医学科</option>
<option>药剂科</option>
<option>护理科</option>
<option>体检科</option>
<option>急诊科</option>
<option>公共卫生与预防科</option>
<option>设备科</option>
<option>财务科</option>
        </select>
        <input type="button" value="医生查询" onclick="searchDoc()">
        </div><br><br>
        <table>
            <thead>
                <tr>
                    <th align="center">选择</th>
                    <th align="center">值班日期</th>
                    <th align="center">午别</th>
                    <th align="center">真实姓名</th>
                    <th align="center">医生职称</th>
                    <th align="center">所在科室</th>
                    <th align="center">挂号级别</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
  <script>
  $(function(){
	  //自动获取发票号
	  $.post(
		  "/nextInvoice",
		  function(res){
			  console.log(res);
			  if(res==""||res==null)
				  {
				  alert("发票已经用完!");
				  }
			  else{
			  $("#invoice").val(res);
			  }
		  }    	         
	  );
	  //自动获取病历号
    	  $.post(
    		  "/nextRegister",
    		  function(res){
    			  $("#caseNumber").val(parseInt(res)+1);
    			  }       	         
    	     );     
           })
     //挂号
     function register(){
	  $.post(
		"/getMaxID",
		function(re)
		{
			var id=re+1;
	   $.post(
		"/getIDByConstantName",
		{"constantName":$("input[name='sex']:checked").val()},
		function(res){
			var gender=res;
			console.log("gender="+gender);
			$.post(
		    		"/getIDByDeptName",
		    		{"deptName":$("#dept option:selected").val()},
		    		function(res1){
		    			var deptID=res1;
		    			console.log("deptID="+deptID);
		    			$.post(
		    				"/getIDByRegistName",
		    				{"registName":$("#registLevel option:selected").val()},
		    				function(res2){
		    					var registLeID=res2;
		    					var userID=parseInt($(".check:checked").parents("tr").children("td:eq(3)").text());
		    					var caseNumber=$("#caseNumber").val();
		    					var realName=$("#realName").val();
		    					var birthDate=new Date(Date.parse(Format($("#birthDate").val(),"yyyy-MM-dd")));
		    					var age=parseInt($("#age").val());
		    					var ageType=$("#ageType option:selected").val();
		    					var visitDate=new Date();
		    					console.log(typeof visitDate);
		    					var noon=$("#noon").text();
		    					var settleID=1;
		    					var registerID=301;
		    					console.log(id);
		    					console.log(caseNumber);
		    					console.log(realName);
		    					console.log(gender);
		    					console.log(birthDate);
		    					console.log(age);
		    					console.log(ageType);
		    					console.log(visitDate);
		    					console.log(noon);
		    					console.log(deptID);
		    					console.log(userID);
		    					console.log(registLeID);
		    					console.log(settleID);
		    					console.log(registerID);
		 
		    					$.ajax({
		    						url:"/insertRegister",
		    						data:{"id":id,"caseNumber":caseNumber,"realName":realName,"gender":gender,"birthDate":birthDate,"age":age,"ageType":ageType,"visitDate":visitDate,"noon":noon,"deptID":deptID,"userID":userID,"registLeID":registLeID,"settleID":settleID,"registerID":registerID},
		    						type:"post",
		    						success:function(){
		    							alert("挂号成功!");
		    							location.reload();
		    						}
		    					})
		    				});
		                 });
		               });
		});
  }
    //显示看诊时间
     var mydate=Format(new Date(),"yyyy-MM-dd");
     $("#nowDate").text(mydate);
     if(new Date().getHours()<12||(new Date().getHours()==12&&new Date().getMinutes()==0&&new Date().getSeconds()==0)){
    	 $("#noon").text("上午");
     }
     else{
    	 $("#noon").text("下午");
     }
  
     //清空发票号
     function reset(){
    	 $("#invoice").val("");
     }
    //选择出生日期，相应的年龄改变
    function getAge(){
    	var str=$("#birthDate").val().split("-");
        var year1=Number(str[0]);
        var month1=Number(str[1]);
        var day1=Number(str[2]);
        var year2=new Date().getFullYear();
        var month2=new Date().getMonth()+1;
        var day2=new Date().getDate();
        if(year2-year1>=2||(year2-year1==1&&month2-month1>1)||(year2-year1==1&&month2-month1==0&&day2-day1>=0))
        {
            $("#ageType").find("option:contains('岁')").attr("selected",true);
           if(month2>month1||(month2==month1&&day2>=day1)){
                $("#age").val(year2-year1);   
            }
            else{
                $("#age").val(year2-year1-1)
            }
        }
        else if((year2-year1)*12+month2-month1<12&&(year2-year1)*12+month2-month1>=1&&day2>day1){
           $("#ageType").find("option:contains('月')").attr("selected",true);
            $("#age").val((year2-year1)*12+month2-month1);
        }
       
        else if(year2==year1&&month2==month1){
                $("#age").val(day2-day1);
                $("#ageType").find("option:contains('天')").attr("selected",true);
            }
       else{
                $("#age").val(31-day1+day2);
                $("#ageType").find("option:contains('天')").attr("selected",true);
            }
    }
        //查询病历
     function searchCase(){
    	 var caseNumber=$("#caseNumber").val();
    	 $.post(
    		"/getRegisterByCaseNumber",
    		{"caseNumber":caseNumber},
    		function(res){
    			console.log(res.constantName);
    			$("#realName").val(res.realName);
    			//判断查询的病人的性别
    			if(res.constantName=="男"){
    				$("#male").attr("checked",true);
    			}
    			else if(res.constantName=="女"){
    				$("#famale").attr("checked",true);
    			}
    			else{
    				alert("查询性别出错！");
    			}
    			var date=Format(res.birthDate,"yyyy-MM-dd");
    			console.log(date);
    			$("#birthDate").val(date);
    			$("#age").val(res.age);
    			$("#ageType").find("option:contains("+res.ageType+")").attr("selected",true);
    			$("#registLe").find("option:contains("+res.registName+")").attr("selected",true);
    			$("#dept").find("option:contains("+res.deptName+")").attr("selected",true);
    		}    	 
    	 );
     }
        //医生查询
     function searchDoc(){
        	$("tr").slice(1).empty();
        	
    	 var deptName=$("#dept option:selected").val();
    	 var registName=$("#registLevel option:selected").val();
    	 console.log(typeof registName);
    	 $.post(
    		"/getIDByDeptName",
    		{"deptName":deptName},
    		function(res){
    			var deptID=res;
    			console.log(deptID);
    			$.post(
    				"/getIDByRegistName",
    				{"registName":registName},
    				function(res2){
    					var registLeID=res2;
    					console.log(registLeID);
    					$.post(
    						"/getScheduling",
    						{"deptID":deptID,"registLeID":registLeID},
    						function(res3){
    							var count=0;
    							$.each(res3,function(i,ele){
    								
    								if(Format(ele.schedDate,"yyyy-MM-dd")>Format(new Date(),"yyyy-MM-dd")||(Format(ele.schedDate,"yyyy-MM-dd")==Format(new Date(),"yyyy-MM-dd")&&$("#noon").text()=="上午"))
    								{
    									count++;
    									var value="<tr class='doc'>"+
    									"<td align='center'><input type='checkbox' class='check'></td>"+
    									"<td align='center'>"+Format(ele.schedDate,"yyyy-MM-dd")+"</td>"+
    									"<td align='center'>"+ele.noon+"</td>"+
    									"<td align='center'>"+ele.userID+"</td>"+
    									"<td align='center'>"+ele.realName+"</td>"+
    									"<td align='center'>"+ele.constantName+"</td>"+
    									"<td align='center'>"+ele.deptName+"</td>"+
    									"<td align='center'>"+ele.registName+"</td>"+"</tr>";
    									$("table").append(value);
    									$(".doc").each(function(){
    										$(this).children("td:eq(3)").hide();
    									})
    								}
    							    
    								});
    						      if(count==0){
    						    	  alert("无匹配条件的医生");
    						      }
    							})
    						});
    				});
    			
    		}
    	
     //时间格式转换函
     function Format(datetime,fmt) {
        if (parseInt(datetime)==datetime) {
            if (datetime.length==10) {
                datetime=parseInt(datetime)*1000;
            } else if(datetime.length==13) {
                datetime=parseInt(datetime);
            }
        }
        datetime=new Date(datetime);
        var o = {
            "M+" : datetime.getMonth()+1,                 //月份
            "d+" : datetime.getDate(),                    //日
            "h+" : datetime.getHours(),                   //小时
            "m+" : datetime.getMinutes(),                 //分
            "s+" : datetime.getSeconds(),                 //秒
            "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
            "S"  : datetime.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
}
  </script>
</body>
</html>